<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null">
      <!-- 搜索区域 -->
      <a-form layout="horizontal">
        <div :class="advanced ? null: 'fold'">
          <a-row>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="法人行"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-select
                          mode="default"
                          :allowClear="true"
                          style="width: 100%"
                          @change="handleInstChange"
                          v-model="queryParams.instId">
                  <a-select-option v-for="r in instIdData" :key="r.label">{{r.label}}-{{r.value}}</a-select-option>
                </a-select>
<!--                <a-input v-if="!this.user.admin" v-model="queryParams.instId" :value="this.user.instId"
                         readonly="readonly"/>-->
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24" v-show="queryParams.instId!=''">
              <a-form-item
                label="微信通道类型"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-select
                  show-search
                  placeholder="请输入代码或名称"
                  option-filter-prop="children"
                  style="width: 100%"
                  :filter-option="filterOption"
                  @focus="selectWxChannel"
                  @search="selectWxChannel"
                  v-model="queryParams.dstChannelType">

                  <a-select-option v-for="(d,index) in suData" v-if="d.id.includes('WX')" :key="d.id">
                    {{ d.id }}-{{d.name}}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row v-show="queryParams.instId!=''">
            <a-col :md="12" :sm="24">
              <a-form-item label="最终交易时间"
                           :labelCol="{span: 5}"
                           :wrapperCol="{span: 18, offset: 1}">
                <a-range-picker
                  format="YYYYMMDD"
                  style="width: 415px;"
                  v-model="queryParams.finalTime"
                :disabledDate="disabledDate"
                @calendarChange="calendarChange"

                @chane="changePicker"/>
              </a-form-item>
            </a-col>
          </a-row>

        </div>
        <div style="text-align: center">
        <span  v-show="queryParams.instId!=''">
          <a-button type="primary" @click="search">查询</a-button>
          <a-button style="margin-left: 8px" @click="reset">重置</a-button>
        </span>
        </div>
      </a-form>
    </div>
    <div v-show="queryParams.instId!=''">
      <!-- 表格区域 -->
      <a-table ref="TableInfo"
               :columns="columns"
               :rowKey="record => record.id"
               :dataSource="dataSource"
               :pagination="pagination"
               :loading="loading"
               :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               :scroll="{ x: 1200 }"
               @change="handleTableChange">
        <template slot="email" slot-scope="text, record">
          <a-popover placement="topLeft">
            <template slot="content">
              <div>{{text}}</div>
            </template>
            <p style="width: 150px;margin-bottom: 0">{{text}}</p>
          </a-popover>
        </template>

      </a-table>

    </div>
  </a-card>
</template>

<script>
  import RangeDate from '@/components/datetime/RangeDate'
  import {mapState} from "vuex"
  import moment from "moment";
  moment.locale('zh-cn');


  export default {
    name: "WechatRisk",
    components: {RangeDate},
    data() {
      return {
        suData: [],
        advanced: false,
        noMerchantInfo: {
          visiable: false,
          data: {}
        },

        queryParams: {
          instId: '',
          // mchntNo: '',
          // status: '',
          finalTime: [],
          startDate:'',
          endDate:'',
          dstChannelId:'',
          dstChannelType:''
        },
        dataSource: [],
        sortedInfo: null,
        paginationInfo: null,
        selectedRowKeys: [],
        pagination: {
          pageSizeOptions: ['10', '20', '30', '40', '100'],
          defaultCurrent: 1,
          defaultPageSize: 10,
          showQuickJumper: true,
          showSizeChanger: true,
          showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
        },
        loading: false,
        instIdData: [],
      }
    },
    computed: {
      ...mapState({
        user: state => state.account.user,

      }),
      columns() {
        let {sortedInfo} = this
        sortedInfo = sortedInfo || {}
        let {filteredInfo} = this
        filteredInfo = filteredInfo || {}
        return [{
          title: '商户号',
          dataIndex: 'mchntNo',
          scopedSlots: {customRender: 'mchntNo'},
        }, {
          title: '商户名称',
          dataIndex: 'mchntName',
          scopedSlots: {customRender: 'mchntName'},
        }, {
          title: '微信商户号',
          dataIndex: 'wechatMchntNo',
          scopedSlots: {customRender: 'wechatMchntNo'},
        }, {
          title: '处罚时间',
          dataIndex: 'punishTime',
          scopedSlots: {customRender: 'punishTime'},
        }, {
          title: '处理方式',
          dataIndex: 'handleMethod',
          scopedSlots: {customRender: 'handleMethod'},
        }, {
          title: '处理状态',
          dataIndex: 'handleState',
          scopedSlots: {customRender: 'handleState'},
        }, {
          title: '法人行',
          dataIndex: 'instId',
          scopedSlots: {customRender: 'instId'},
        }]
      }
    },
    mounted() {
      this.$get('admin/upmporg/getInstList').then((r) => {
        this.instIdData = r.data
      })
    },
    methods: {

      changePicker (a, b) {
        this.queryParams.finalTime= a.length ? b : []
      },
      // 处理打开日期面板事件
   /*   openChange (val) {
        if (val) {
          this.selectCurrentDate = this.queryParams.finalTime.length ? this.queryParams.finalTime[0] : null
        }
      },*/
      // 设置禁用时间段
      disabledDate (current, date) {
        if (this.selectCurrentDate != null) {
          // 在开始时间的前后范围31天
          return current < moment(this.selectCurrentDate).add(-30, 'days') || current > moment(this.selectCurrentDate).add(30, 'days')
        }
        return false
      },
      // 点击面板时间事件
      calendarChange (date, dateString) {
        if (date.length <= 1) {
          this.selectCurrentDate = date[0]
        } else {
          this.selectCurrentDate = null
        }
      },

      selectWxChannel(val) {
        this.suData = []
        let param = {}
        if (val) {
          param = {
            q: encodeURIComponent(val + `&${this.queryParams.instId}`),
            pageSize: 20
          }
          if (val.length >= 5) {
            this.$get('/admin/merParam/wechatRisk/findPDstChannelCfgList', param).then((r) => {
              let result = r.data.results
            })
          }
        } else {
          param = {
            q: encodeURIComponent(`&${this.queryParams.instId}`),
            pageSize: 20
          }
          this.$get('/admin/merParam/wechatRisk/findPDstChannelCfgList', param).then((r) => {
              this.suData= r.data.results
          })
        }
      },

      getMoment(value) {
        return this.$moment(value).format('YYYYMMDD')
      },

      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },

      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },

      handleInstChange(value) {
        this.queryParams.instId = value;
      /*  if (value != "") {
          let params = {}
          params.instId = value;
          this.$get('admin/merParam/wechatRisk', {...params}).then((r) => {
            this.dataSource = r.data.rows;
            this.search();
          })
        }*/
      },

      warning(message) {
        const h = this.$createElement;
        this.$warning({
          title: '提示',
          content: h('div', {}, [
            h('p', message),
          ]),
          onOk() {},
        });
      },

      search() {
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }

        let {sortedInfo, filteredInfo} = this
        let sortField, sortOrder
        // 获取当前列的排序和列的过滤规则
        if (sortedInfo) {
          sortField = sortedInfo.field
          sortOrder = sortedInfo.order
        }
        let finalTime = this.queryParams.finalTime
        if(!finalTime){
          this.warning('交易时间不能为空')
          return;
        }
        console.log(finalTime)
        if (finalTime) {
          this.queryParams.startDate = this.getMoment(finalTime[0])
          this.queryParams.endDate = this.getMoment(finalTime[1])
        }
        if(!this.queryParams.dstChannelType){
          this.warning('微信通道类型不能为空')
          return
        }


        this.queryParams.dstChannelId='WXZF'
        let params = this.queryParams

        this.fetch({
          sortField: sortField,
          sortOrder: sortOrder,
          ...params,
          ...filteredInfo
        })
      },
      reset() {
        // 取消选中
        this.selectedRowKeys = []
        // 重置分页
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }
        // 重置列排序规则
        this.sortedInfo = null
        // 重置查询参数
        this.queryParams = {}
        // // 清空时间选择
        // this.$refs.createTime.reset()
        this.fetch()
      },
      handleTableChange(pagination, filters, sorter) {
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }

        // 将这三个参数赋值给Vue data，用于后续使用
        this.paginationInfo = pagination
        this.filteredInfo = filters
        this.sortedInfo = sorter

        this.fetch({
          sortField: sorter.field,
          sortOrder: sorter.order,
          ...this.queryParams,
          ...filters
        })
      },
      fetch(params = {}) {
        this.loading = true
        if (this.paginationInfo) {
          // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
          this.$refs.TableInfo.pagination.current = this.paginationInfo.current
          this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
          params.pageSize = this.paginationInfo.pageSize
          params.pageNum = this.paginationInfo.current
        } else {
          // 如果分页信息为空，则设置为默认值
          params.pageSize = this.pagination.defaultPageSize
          params.pageNum = this.pagination.defaultCurrent
        }
        this.$get('admin/merParam/wechatRisk/list', {
          ...params
        }).then((r) => {
          let data = r.data
          const pagination = {...this.pagination}
          pagination.total = data.total
          this.dataSource = data.rows
          this.pagination = pagination
          this.loading = false
        })
      },
    }
  }
</script>

<style lang="less" scoped>
  @import "../../../../../../static/less/Common";
</style>

